package cn.seocoo.platform.api.controller.user;

import cn.seocoo.platform.api.util.JSONResult;
import cn.seocoo.platform.common.constant.ClassContants;
import cn.seocoo.platform.common.core.domain.AjaxResult;
import cn.seocoo.platform.common.utils.StringUtils;
import cn.seocoo.platform.user.dto.UnitFrom;
import cn.seocoo.platform.user.dto.UserFrom;
import cn.seocoo.platform.user.service.IAppUserService;
import cn.seocoo.platform.validatecode.domain.Validatecode;
import cn.seocoo.platform.validatecode.service.ValidatecodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @ClassName: ApiUserAuthController
 * @description: 用户认证
 * @author: wujian
 * @Date: 2019-12-29 17:20
 **/
@RequestMapping("api/user")
@RestController
public class ApiUserAuthController {

    @Autowired
    private IAppUserService iAppUserService;

    @Autowired
    private ValidatecodeService validatecodeService;

    /**
     * @return cn.seocoo.platform.api.util.JSONResult
     * @Author WuJian
     * @Description //个人用户户认证
     * @Date 17:40 2019/12/29
     * @Param []
     **/
    @PostMapping("/autherUser")
    public JSONResult autherUser(@RequestBody UserFrom userFrom) {
        if (StringUtils.isBlank(userFrom.getRealName())) {
            return JSONResult.errorMsg("请填写真实姓名");
        }
        if (StringUtils.isBlank(userFrom.getIdNumber())) {
            return JSONResult.errorMsg("请填写身份证号");
        }
        if (StringUtils.isBlank(userFrom.getIdPhotoFont())) {
            return JSONResult.errorMsg("请上传身份证正面");
        }
        if (StringUtils.isBlank(userFrom.getIdPhotoReverse())) {
            return JSONResult.errorMsg("请上传身份证反面");
        }
        if (StringUtils.isBlank(userFrom.getSex())) {
            return JSONResult.errorMsg("请选择性别");
        }
        if (userFrom.getAppUserId() == null) {
            return JSONResult.errorMsg("申请用户不能为空");
        }

        try {
            Map<String, Object> map = iAppUserService.save(userFrom);
            return JSONResult.ok(map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JSONResult.errorMsg(JSONResult.ERROR_MSG);


    }


    /**
     * @return cn.seocoo.platform.api.util.JSONResult
     * @Author WuJian
     * @Description //商户认证
     * @Date 17:40 2019/12/29
     * @Param []
     **/
    @PostMapping("/autherUnit")
    public JSONResult autherUnit(@RequestBody UnitFrom unitFrom) {
        if (StringUtils.isBlank(unitFrom.getUnitName())) {
            return JSONResult.errorMsg("请填写企业名称");
        }
        if (StringUtils.isBlank(unitFrom.getCreditCode())) {
            return JSONResult.errorMsg("请填写信用代码证号");
        }
        if (StringUtils.isBlank(unitFrom.getAccountNumber())) {
            return JSONResult.errorMsg("请填写对公账号");
        }
        if (StringUtils.isBlank(unitFrom.getUnitEmail())) {
            return JSONResult.errorMsg("请填写邮箱");
        }
        if (StringUtils.isBlank(unitFrom.getUnitPhoneNumber())) {
            return JSONResult.errorMsg("请填写联系电话");
        }
        if (StringUtils.isBlank(unitFrom.getUnitAddress())) {
            return JSONResult.errorMsg("请填写详细地址");
        }
        if (StringUtils.isBlank(unitFrom.getBusinessLicense())) {
            return JSONResult.errorMsg("请上传营业执照");
        }
        if (StringUtils.isBlank(unitFrom.getValidateCode())) {
            return JSONResult.errorMsg("请填写验证码");
        }
        if (StringUtils.isBlank(unitFrom.getAreaCode())) {
            return JSONResult.errorMsg("区域地址不能为空");
        }
        if (unitFrom.getAppUserId() == null) {
            return JSONResult.errorMsg("申请用户不能为空");
        }

        try {
            Validatecode validatecode = new Validatecode();
            validatecode.setPhone(unitFrom.getUnitPhoneNumber());
            validatecode.setValidateCode(unitFrom.getValidateCode());
            boolean flag = validatecodeService.checkValidatecode(validatecode);
            if (!flag) {
                return JSONResult.errorMsg(ClassContants.VALIDATE_CODE_ERROR);
            }else{
                Integer row = iAppUserService.autherUnit(unitFrom);
                if (row > 0) {
                    return JSONResult.ok(JSONResult.SUBMIT_SUCCESS_MSG);
                } else {
                    return JSONResult.ok(JSONResult.SUBMIT_ERROR_MSG);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return JSONResult.errorMsg(JSONResult.ERROR_MSG);


    }


}
